<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Examples  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a title="Examples  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">SwiftyMocky 4.2.0 Docs</a> (86% documented)</p>
        <p class="header-right"><a href="https://github.com/MakeAWishFoundation/SwiftyMocky"><img src="img/gh.png" alt="GitHub"/>View on GitHub</a></p>
        <div class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </div>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">SwiftyMocky Reference</a>
        <img id="carat" src="img/carat.png" alt=""/>
        Examples  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Main%20Guides.html">Main Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="overview.html">Overview</a>
              </li>
              <li class="nav-group-task">
                <a href="installation.html">Installation</a>
              </li>
              <li class="nav-group-task">
                <a href="setup-in-project.html">Setup in project</a>
              </li>
              <li class="nav-group-task">
                <a href="mockfile.html">Mockfile</a>
              </li>
              <li class="nav-group-task">
                <a href="command-line-interface.html">Command Line Interface</a>
              </li>
              <li class="nav-group-task">
                <a href="supported-features.html">Supported features</a>
              </li>
              <li class="nav-group-task">
                <a href="prototyping.html">Prototyping</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Additional%20Guides.html">Additional Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="matcher-support-for-not-equatable.html">Matcher support for not Equatable</a>
              </li>
              <li class="nav-group-task">
                <a href="handling-generics.html">Handling Generics</a>
              </li>
              <li class="nav-group-task">
                <a href="examples.html">Examples</a>
              </li>
              <li class="nav-group-task">
                <a href="legacy.html">Legacy</a>
              </li>
              <li class="nav-group-task">
                <a href="add-xcode-generate-action.html">Add XCode generate action</a>
              </li>
              <li class="nav-group-task">
                <a href="known-issues.html">Known issues</a>
              </li>
              <li class="nav-group-task">
                <a href="changelog.html">CHANGELOG</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Global%20methods.html">Global methods</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky5Givenyyx_ABQzAA14StubbingPolicyOtAA4MockRzlF">Given(_:_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky5Givenyyxm_06StaticC0QzAA14StubbingPolicyOtAA0D4MockRzlF">Given(_:_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky7Performyyx_ABQztAA4MockRzlF">Perform(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky7Performyyxm_06StaticC0QztAA0D4MockRzlF">Perform(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify__4file4lineyx_ABQzs12StaticStringVSutAA4MockRzlF">Verify(_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify__4file4lineyxm_06StaticC0Qzs0F6StringVSutAA0F4MockRzlF">Verify(_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify___4file4lineyx_AA5CountOABQzs12StaticStringVSutAA4MockRzlF">Verify(_:_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify___4file4lineyxm_AA5CountO06StaticC0Qzs0G6StringVSutAA0G4MockRzlF">Verify(_:_:_:file:line:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Configuration.html">Configuration</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/StubbingPolicy.html">StubbingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/SequencingPolicy.html">SequencingPolicy</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Types.html">Types</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/Mock.html">Mock</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/StaticMock.html">StaticMock</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Parameter.html">Parameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher.html">Matcher</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher/ComparisonResult.html">– ComparisonResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher/ParameterComparisonResult.html">– ParameterComparisonResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Countable.html">Countable</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Count.html">Count</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/Stubber.html">Stubber</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/StubberThrows.html">StubberThrows</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Generics.html">Generics</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/GenericAttribute.html">GenericAttribute</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Helpers.html">Helpers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Helpers.html#/s:11SwiftyMocky20XCTAssertThrowsError_2of_4file4lineyxyKXK_q_mSSyXKs12StaticStringVSuts0E0R_r0_lF">XCTAssertThrowsError(_:of:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Helpers.html#/s:11SwiftyMocky20XCTAssertThrowsError_5error_4file4lineyxyKXK_q_SSyXKs12StaticStringVSutSQR_s0E0R_r0_lF">XCTAssertThrowsError(_:error:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/MockyAssertion.html">MockyAssertion</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Internal.html">Internal</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/SwiftyMockyTestObserver.html">SwiftyMockyTestObserver</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/SwiftyMockyTestObserver.html">SwiftyMockyTestObserver</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/FatalErrorUtil.html">FatalErrorUtil</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/OptionalType.html">OptionalType</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithSequencingPolicy.html">WithSequencingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithStaticSequencingPolicy.html">WithStaticSequencingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithStubbingPolicy.html">WithStubbingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Int.html">Int</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Optional.html">Optional</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UInt.html">UInt</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/MockError.html">MockError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/StubProduct.html">StubProduct</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/StubbedMethod.html">StubbedMethod</a>
              </li>
              <li class="nav-group-task">
                <a href="Internal.html#/s:11SwiftyMocky7Failureys5NeverOSSF">Failure(_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Internal.html#/s:11SwiftyMocky0B6Assert__4file4lineySbyXK_SSyXKs12StaticStringVSutF">MockyAssert(_:_:file:line:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Guides.html">Other Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="contents.html">Contents</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Classes.html">Other Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/ArgumentCaptor.html">ArgumentCaptor</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Enums.html">Other Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/MockScope.html">MockScope</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Utils.html">Utils</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Protocols.html">Other Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/TypeErasedValue.html">TypeErasedValue</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Structs.html">Other Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/TypeErasedAttribute.html">TypeErasedAttribute</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            
            <h1 id='examples' class='heading'>Examples</h1>

<p>All examples are part of example project, which contains more examples.</p>
<h2 id='table-of-contents' class='heading'>Table of Contents:</h2>

<ol>
<li><a href="#example1">Example 1:</a> basic cases

<ul>
<li><a href="#example1.1">Simple protocol with methods</a></li>
<li><a href="#example1.2">Simple protocol with methods - optional attributes</a></li>
<li><a href="#example1.3">Simple protocol with properties</a></li>
<li><a href="#example1.4">Protocol, that adopts other protocols</a></li>
</ul></li>
<li><a href="#example2">Example 2:</a> throwing

<ul>
<li><a href="#example2.1">Simple protocol with methods that throws</a></li>
</ul></li>
<li><a href="#example3">Example 3:</a> custom attributes

<ul>
<li><a href="#example3.1">Simple protocol with methods using tuples</a></li>
<li><a href="#example3.2">Simple protocol with methods using custom type</a></li>
</ul></li>
<li><a href="#example4">Example 4:</a> static

<ul>
<li><a href="#example4.1">Simple protocol with static methods and properties</a></li>
</ul></li>
<li><a href="#example5">Example 5:</a> closures as attributes

<ul>
<li><a href="#example5.1">Working with non escaping closures</a></li>
<li><a href="#example5.2">Working with escaping closures</a></li>
<li><a href="#example5.3">Using completion block based approach</a></li>
</ul></li>
<li><a href="#example6">Example 6:</a> protocol with initializers requirement</li>
<li><a href="#example7">Example 7:</a> generics

<ul>
<li><a href="#example7.1">Protocol with generic methods</a></li>
<li><a href="#example7.2">Protocol with associated types</a></li>
</ul></li>
</ol>
<h2 id='a-name-example1-a-example-1' class='heading'><a name="example1"></a> Example 1:</h2>

<p>Simple protocols with example of setting up and verifying methods and properties.</p>
<h3 id='a-name-example1-1-a-simple-protocol-with-methods' class='heading'><a name="example1.1"></a> Simple protocol with methods</h3>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolWithMethods</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">simpleMethod</span><span class="p">()</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">String</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">String</span><span class="p">?</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> to specify stubbed methods return values, based on passed attributes, and <code>Verify</code> to check invocations count:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">SimpleProtocolWithMethodsMock</span><span class="p">()</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span> <span class="c1">// Should be 0</span>

<span class="c1">// Regardless of registrations order, first will check most explicit given values</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"a"</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="s">"A"</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"b"</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="s">"B"</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="s">"default"</span><span class="p">))</span>

<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="s">"any parameter"</span><span class="p">),</span> <span class="s">"default"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="s">"a"</span><span class="p">),</span> <span class="s">"A"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="s">"b"</span><span class="p">),</span> <span class="s">"B"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="s">"any parameter"</span><span class="p">),</span> <span class="s">"default"</span><span class="p">)</span>

<span class="n">mock</span><span class="o">.</span><span class="nf">simpleMethod</span><span class="p">()</span>
<span class="n">mock</span><span class="o">.</span><span class="nf">simpleMethod</span><span class="p">()</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMethod</span><span class="p">())</span> <span class="c1">// Should be 1+</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span> <span class="c1">// Total method invocations count should be 4</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"any parameter"</span><span class="p">)))</span> <span class="c1">// Should be called twice wth "any parameter"</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"a"</span><span class="p">)))</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"b"</span><span class="p">)))</span>
</code></pre>
<h3 id='a-name-example1-2-a-simple-protocol-with-methods-optional-attributes' class='heading'><a name="example1.2"></a> Simple protocol with methods - optional attributes</h3>

<p>When handling optional attributes, it requires to register types in Matcher, in order to perform proper check of attributes values (for now - will simplify that in 2.0). As long as you are using Parameter.value - you should register its type.</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolWithMethods</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">simpleMethod</span><span class="p">()</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">String</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">String</span><span class="p">?</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> and <code>Verify</code> constrained by optional parameter value:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">SimpleProtocolWithMethodsMock</span><span class="p">()</span>

<span class="c1">// As String? is not recognized as Equatable, we need to register comparator on matcher</span>
<span class="n">mock</span><span class="o">.</span><span class="n">matcher</span><span class="o">.</span><span class="nf">register</span><span class="p">((</span><span class="kt">Optional</span><span class="o">&lt;</span><span class="kt">String</span><span class="o">&gt;</span><span class="p">)</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$0</span> <span class="o">==</span> <span class="nv">$1</span> <span class="p">}</span> <span class="c1">// Could use Matcher.default instead, as it is default matcher for all mocks</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span> <span class="c1">// Should be 0</span>

<span class="c1">// When same level of explicity - last given is matched first</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="kc">nil</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">nil</span><span class="p">))</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kc">nil</span><span class="p">),</span> <span class="kc">nil</span><span class="p">)</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="s">"some"</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="s">"not nil"</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="kc">nil</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="s">"is nil"</span><span class="p">))</span>

<span class="kt">XCTAssertNotEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kc">nil</span><span class="p">),</span> <span class="kc">nil</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kc">nil</span><span class="p">),</span> <span class="s">"is nil"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="s">"some"</span><span class="p">),</span> <span class="s">"not nil"</span><span class="p">)</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">.</span><span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="kc">nil</span><span class="p">)))</span>
</code></pre>
<h3 id='a-name-example1-3-a-simple-protocol-with-properties' class='heading'><a name="example1.3"></a> Simple protocol with properties</h3>

<p>As it is impossible to auto-generate default values for properties, for all properties that are not optional (or are implicitely unwrapped) default values should be set manually, before usage. Otherwise, fatalError will occur.</p>

<p>By default, we auto generate both getters and setters for methods (regardless of its get/set attributes).</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolWithProperties</span> <span class="p">{</span>
    <span class="k">var</span> <span class="nv">property</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">weak</span> <span class="k">var</span> <span class="nv">weakProperty</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="o">!</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyGetOnly</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyOptional</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyImplicit</span><span class="p">:</span> <span class="kt">Int</span><span class="o">!</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>

<p>Test - setup mock:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">SimpleProtocolWithPropertiesMock</span><span class="p">()</span>

<span class="c1">// We should set all initial values for non optional parameters and implicitly unwrapped optional parameters</span>
<span class="n">mock</span><span class="o">.</span><span class="n">property</span> <span class="o">=</span> <span class="s">"some property"</span>
<span class="n">mock</span><span class="o">.</span><span class="n">propertyGetOnly</span> <span class="o">=</span> <span class="s">"get only ;)"</span>
<span class="n">mock</span><span class="o">.</span><span class="n">propertyImplicit</span> <span class="o">=</span> <span class="mi">1</span>

<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="n">property</span><span class="p">,</span> <span class="s">"some property"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="n">propertyGetOnly</span><span class="p">,</span> <span class="s">"get only ;)"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="n">propertyOptional</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="n">propertyImplicit</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="n">mock</span><span class="o">.</span><span class="n">propertyOptional</span> <span class="o">=</span> <span class="mi">2</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="n">propertyOptional</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</code></pre>
<h3 id='a-name-example1-4-a-protocol-that-adopts-other-protocols' class='heading'><a name="example1.4"></a> Protocol, that adopts other protocols</h3>

<p>By default, SwiftyMocky will generate Mock, adopting all methods/properties gathered from all protocols, so it will support</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolWithMethods</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">simpleMethod</span><span class="p">()</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">String</span>
    <span class="kd">func</span> <span class="nf">simpleMehtodThatReturns</span><span class="p">(</span><span class="nv">optionalParam</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">String</span><span class="p">?</span>
<span class="p">}</span>

<span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolWithProperties</span> <span class="p">{</span>
    <span class="k">var</span> <span class="nv">property</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">weak</span> <span class="k">var</span> <span class="nv">weakProperty</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="o">!</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyGetOnly</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyOptional</span><span class="p">:</span> <span class="kt">Int</span><span class="p">?</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">propertyImplicit</span><span class="p">:</span> <span class="kt">Int</span><span class="o">!</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">SimpleProtocolThatInheritsOtherProtocols</span><span class="p">:</span> <span class="kt">SimpleProtocolWithMethods</span><span class="p">,</span> <span class="kt">SimpleProtocolWithProperties</span> <span class="p">{</span>

<span class="p">}</span>
</code></pre>

<p>Test - setup mock:</p>
<pre class="highlight swift"><code><span class="kd">func</span> <span class="nf">test_simpleProtocol_that_inherits</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">SimpleProtocolThatInheritsOtherProtocolsMock</span><span class="p">()</span>

    <span class="kt">XCTAssert</span><span class="p">(</span><span class="n">mock</span> <span class="k">is</span> <span class="kt">SimpleProtocolWithProperties</span><span class="p">)</span>
    <span class="kt">XCTAssert</span><span class="p">(</span><span class="n">mock</span> <span class="k">is</span> <span class="kt">SimpleProtocolWithMethods</span><span class="p">)</span>
    <span class="o">...</span>
<span class="p">}</span>
</code></pre>
<h2 id='a-name-example2-a-example-2' class='heading'><a name="example2"></a> Example 2:</h2>

<p>Simple protocol that declares methods that throws. For rethrowing methods valid signatures are generated, but there is no possibility to specify thrown error value.</p>
<h3 id='a-name-example2-1-a-simple-protocol-with-methods-that-throws' class='heading'><a name="example2.1"></a> Simple protocol with methods that throws</h3>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithThrowingMethods</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">methodThatThrows</span><span class="p">()</span> <span class="k">throws</span>
    <span class="kd">func</span> <span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> to specify stubbed methods return values and throws errors:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithThrowingMethodsMock</span><span class="p">()</span>

<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="mi">200</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">true</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="mi">404</span><span class="p">),</span> <span class="nv">willThrow</span><span class="p">:</span> <span class="kt">SimpleTestError</span><span class="o">.</span><span class="n">failure</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willThrow</span><span class="p">:</span> <span class="kt">SimpleTestError</span><span class="o">.</span><span class="n">other</span><span class="p">))</span>

<span class="kt">XCTAssertNoThrow</span><span class="p">(</span><span class="k">try</span> <span class="n">mock</span><span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">200</span><span class="p">),</span> <span class="s">"Should not throw"</span><span class="p">)</span>
<span class="kt">XCTAssertThrowsError</span><span class="p">(</span><span class="k">try</span> <span class="n">mock</span><span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">404</span><span class="p">))</span>
<span class="kt">XCTAssertThrowsError</span><span class="p">(</span><span class="k">try</span> <span class="n">mock</span><span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">123</span><span class="p">))</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatReturnsAndThrows</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
</code></pre>
<h2 id='a-name-example3-a-example-3' class='heading'><a name="example3"></a> Example 3:</h2>

<p>Simple protocol that declares methods that uses more sophisticated data, that plain strings or integers.</p>
<h3 id='a-name-example3-1-a-simple-protocol-with-methods-using-tuples' class='heading'><a name="example3.1"></a> Simple protocol with methods using tuples</h3>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithTuples</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="p">(</span><span class="kt">String</span><span class="p">,</span><span class="kt">Int</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> to specify stubbed methods return values and throws errors:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithTuplesMock</span><span class="p">()</span>

<span class="c1">// When using only .any - no matcher registering needed</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">0</span><span class="p">))</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="p">(</span><span class="s">"first"</span><span class="p">,</span><span class="mi">1</span><span class="p">)),</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1">// When using custom attributes or tuples as .value(...) - registering comparator in Matcher is required!</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">((</span><span class="kt">String</span><span class="p">,</span><span class="kt">Int</span><span class="p">)</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="n">lhs</span><span class="p">,</span> <span class="n">rhs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span> <span class="k">in</span>
    <span class="k">return</span> <span class="n">lhs</span><span class="o">.</span><span class="mi">0</span> <span class="o">==</span> <span class="n">rhs</span><span class="o">.</span><span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">lhs</span><span class="o">.</span><span class="mi">1</span> <span class="o">==</span> <span class="n">rhs</span><span class="o">.</span><span class="mi">1</span>
<span class="p">}</span>

<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">((</span><span class="s">"first"</span><span class="p">,</span><span class="mi">1</span><span class="p">)),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">1</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">((</span><span class="s">"second"</span><span class="p">,</span><span class="mi">2</span><span class="p">)),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">2</span><span class="p">))</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="p">(</span><span class="s">"first"</span><span class="p">,</span><span class="mi">1</span><span class="p">)),</span> <span class="mi">1</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="p">(</span><span class="s">"second"</span><span class="p">,</span><span class="mi">2</span><span class="p">)),</span> <span class="mi">2</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="p">(</span><span class="s">"first"</span><span class="p">,</span><span class="mi">0</span><span class="p">)),</span> <span class="mi">0</span><span class="p">)</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesTuple</span><span class="p">(</span><span class="nv">tuple</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">((</span><span class="s">"first"</span><span class="p">,</span><span class="mi">1</span><span class="p">))))</span>
</code></pre>
<h3 id='a-name-example3-2-a-simple-protocol-with-methods-using-custom-type' class='heading'><a name="example3.2"></a> Simple protocol with methods using custom type</h3>

<p>Protocol and object definition:</p>
<pre class="highlight swift"><code><span class="kd">struct</span> <span class="kt">UserObject</span> <span class="p">{</span>
    <span class="k">let</span> <span class="nv">name</span><span class="p">:</span> <span class="kt">String</span>
    <span class="k">let</span> <span class="nv">surname</span><span class="p">:</span> <span class="kt">String</span>
    <span class="k">let</span> <span class="nv">age</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">}</span>

<span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithCustomAttributes</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">methodThatTakesUser</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="kt">UserObject</span><span class="p">)</span> <span class="k">throws</span>
    <span class="kd">func</span> <span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="kt">UserObject</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> to specify stubbed methods return values and throws errors:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithCustomAttributesMock</span><span class="p">()</span>

<span class="c1">// Register comparing user object</span>
<span class="c1">// We can use registration for Array of elements, which will compare value by value</span>
<span class="c1">// Also, providing by default comparator for element type</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">([</span><span class="kt">UserObject</span><span class="p">]</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="kt">UserObject</span><span class="p">,</span> <span class="nv">rhs</span><span class="p">:</span> <span class="kt">UserObject</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span> <span class="k">in</span>
    <span class="k">guard</span> <span class="n">lhs</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="n">rhs</span><span class="o">.</span><span class="n">name</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">false</span> <span class="p">}</span>
    <span class="k">guard</span> <span class="n">lhs</span><span class="o">.</span><span class="n">surname</span> <span class="o">==</span> <span class="n">rhs</span><span class="o">.</span><span class="n">surname</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">false</span> <span class="p">}</span>
    <span class="k">guard</span> <span class="n">lhs</span><span class="o">.</span><span class="n">age</span> <span class="o">==</span> <span class="n">rhs</span><span class="o">.</span><span class="n">age</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kc">false</span> <span class="p">}</span>
    <span class="k">return</span> <span class="kc">true</span>
<span class="p">}</span>

<span class="k">let</span> <span class="nv">user1</span> <span class="o">=</span> <span class="kt">UserObject</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"Karl"</span><span class="p">,</span> <span class="nv">surname</span><span class="p">:</span> <span class="s">"Gustav"</span><span class="p">,</span> <span class="nv">age</span><span class="p">:</span> <span class="mi">90</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">user2</span> <span class="o">=</span> <span class="kt">UserObject</span><span class="p">(</span><span class="nv">name</span><span class="p">:</span> <span class="s">"Dan"</span><span class="p">,</span> <span class="nv">surname</span><span class="p">:</span> <span class="s">"Dannerson"</span><span class="p">,</span> <span class="nv">age</span><span class="p">:</span> <span class="mi">13</span><span class="p">)</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesUser</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="n">user2</span><span class="p">),</span> <span class="nv">willThrow</span><span class="p">:</span> <span class="kt">UserVerifyError</span><span class="o">.</span><span class="n">tooYoung</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">0</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">([</span><span class="n">user1</span><span class="p">,</span> <span class="n">user2</span><span class="p">]),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">2</span><span class="p">))</span>

<span class="kt">XCTAssertNoThrow</span><span class="p">(</span><span class="k">try</span> <span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesUser</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="n">user1</span><span class="p">),</span> <span class="s">"Should not throw"</span><span class="p">)</span>
<span class="kt">XCTAssertThrowsError</span><span class="p">(</span><span class="k">try</span> <span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesUser</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="n">user2</span><span class="p">))</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="n">user1</span><span class="p">,</span> <span class="n">user2</span><span class="p">]),</span> <span class="mi">2</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="n">user1</span><span class="p">,</span> <span class="n">user2</span><span class="p">,</span> <span class="n">user1</span><span class="p">]),</span> <span class="mi">0</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesArrayOfUsers</span><span class="p">(</span><span class="nv">array</span><span class="p">:</span> <span class="p">[</span><span class="n">user2</span><span class="p">,</span> <span class="n">user1</span><span class="p">]),</span> <span class="mi">0</span><span class="p">)</span>
</code></pre>
<h2 id='a-name-example4-a-example-4' class='heading'><a name="example4"></a> Example 4:</h2>

<p>Protocol that have static members.</p>
<h3 id='a-name-example4-1-a-simple-protocol-with-static-methods-and-properties' class='heading'><a name="example4.1"></a> Simple protocol with static methods and properties</h3>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithStaticMembers</span> <span class="p">{</span>
    <span class="kd">static</span> <span class="k">var</span> <span class="nv">staticProperty</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
    <span class="kd">static</span> <span class="kd">func</span> <span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">}</span>
</code></pre>

<p>Test - usage of <code>Given</code> and <code>Verify</code> with static members.</p>
<pre class="highlight swift"><code><span class="c1">// Static members are handled similar way - but instead of instance</span>
<span class="c1">// you pass its type to Verify and Given calls</span>

<span class="c1">// Static properties should be set with default values - same as with instance ones</span>
<span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="n">staticProperty</span> <span class="o">=</span> <span class="s">"value"</span>

<span class="kt">Given</span><span class="p">(</span><span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">1</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="mi">2</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willThrow</span><span class="p">:</span> <span class="kt">SimpleTestError</span><span class="o">.</span><span class="n">failure</span><span class="p">))</span>

<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="n">staticProperty</span><span class="p">,</span> <span class="s">"value"</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="k">try</span><span class="p">?</span> <span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">0</span><span class="p">),</span> <span class="mi">1</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="k">try</span><span class="p">?</span> <span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
<span class="kt">XCTAssertThrowsError</span><span class="p">(</span><span class="k">try</span> <span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">-</span><span class="mi">3</span><span class="p">))</span>
<span class="kt">XCTAssertThrowsError</span><span class="p">(</span><span class="k">try</span> <span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">2</span><span class="p">))</span>

<span class="kt">Verify</span><span class="p">(</span><span class="kt">ProtocolWithStaticMembersMock</span><span class="o">.</span><span class="k">self</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="o">.</span><span class="nf">staticMethod</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
</code></pre>
<h2 id='a-name-example5-a-example-5' class='heading'><a name="example5"></a> Example 5:</h2>

<p>Protocol that has methods with attributes being closures.</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithClosures</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="nf">methodThatTakes</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">)</span>
    <span class="kd">func</span> <span class="nf">methodThatTakesEscaping</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">(</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">)</span>
    <span class="kd">func</span> <span class="nf">methodThatTakesCompletionBlock</span><span class="p">(</span><span class="nv">completion</span><span class="p">:</span> <span class="p">(</span><span class="kt">Bool</span><span class="p">,</span><span class="kt">Error</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<h3 id='a-name-example5-1-a-working-with-non-escaping-closures' class='heading'><a name="example5.1"></a> Working with non escaping closures</h3>

<p>For non escaping closures - every parameter defined as .value(&hellip;) is always treated as .any</p>

<p>Sample test:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithClosuresMock</span><span class="p">()</span>

<span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakes</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="p">})</span>
<span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakes</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="o">*</span> <span class="mi">2</span> <span class="p">})</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakes</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
<span class="c1">// For non escaping closures - every .value(...) is always treated as .any</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakes</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">({</span> <span class="nv">$0</span> <span class="p">})))</span>
</code></pre>
<h3 id='a-name-example5-2-a-working-with-escaping-closures' class='heading'><a name="example5.2"></a> Working with escaping closures</h3>

<p>There is no limitation as above for escaping closures. Still it makes little sense to compare two closures.</p>

<p>Sample test:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithClosuresMock</span><span class="p">()</span>

<span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesEscaping</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="p">})</span>
<span class="n">mock</span><span class="o">.</span><span class="nf">methodThatTakesEscaping</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="p">{</span> <span class="nv">$0</span> <span class="o">*</span> <span class="mi">2</span> <span class="p">})</span>

<span class="c1">// It is possible to check based on .value(...) for escaping closures</span>
<span class="c1">// It requires to register closure comparator to Matcher</span>
<span class="c1">// Nevertheless - we have not found ane good reason to do that yet :)</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">(((</span><span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span><span class="p">)</span><span class="o">.</span><span class="k">self</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="n">lhs</span><span class="p">,</span> <span class="n">rhs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span> <span class="k">in</span>
    <span class="k">return</span> <span class="nf">lhs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="nf">rhs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nf">lhs</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="nf">rhs</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="p">}</span>

<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesEscaping</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">))</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesEscaping</span><span class="p">(</span><span class="nv">closure</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">({</span> <span class="nv">$0</span> <span class="p">})))</span>
</code></pre>
<h3 id='a-name-example5-3-a-using-completion-block-based-approach' class='heading'><a name="example5.3"></a> Using completion block based approach</h3>

<p>In these cases, we usually need to execute completion block specified as method attribute. For that, we can use <code>Perform</code>, to specify closure, that will execute upon method invocation.</p>

<p>Sample test:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithClosuresMock</span><span class="p">()</span>

<span class="k">let</span> <span class="nv">calledCompletionBlock</span> <span class="o">=</span> <span class="nf">expectation</span><span class="p">(</span><span class="nv">description</span><span class="p">:</span> <span class="s">"Should call completion block"</span><span class="p">)</span>

<span class="c1">// Perform allows to execute given closure, with all the method parameters, as soon as it is being called</span>
<span class="kt">Perform</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodThatTakesCompletionBlock</span><span class="p">(</span><span class="nv">completion</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">perform</span><span class="p">:</span> <span class="p">{</span> <span class="p">(</span><span class="n">completion</span><span class="p">)</span> <span class="k">in</span>
    <span class="nf">completion</span><span class="p">(</span><span class="kc">true</span><span class="p">,</span><span class="kc">nil</span><span class="p">)</span>
<span class="p">}))</span>

<span class="n">mock</span><span class="o">.</span><span class="n">methodThatTakesCompletionBlock</span> <span class="p">{</span> <span class="p">(</span><span class="n">success</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
    <span class="n">calledCompletionBlock</span><span class="o">.</span><span class="nf">fulfill</span><span class="p">()</span>
    <span class="kt">XCTAssertTrue</span><span class="p">(</span><span class="n">success</span><span class="p">)</span>
    <span class="kt">XCTAssertNil</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
<span class="p">}</span>

<span class="nf">waitForExpectations</span><span class="p">(</span><span class="nv">timeout</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="n">error</span><span class="p">)</span> <span class="k">in</span>
    <span class="k">guard</span> <span class="k">let</span> <span class="nv">error</span> <span class="o">=</span> <span class="n">error</span> <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="p">}</span>
    <span class="kt">XCTFail</span><span class="p">(</span><span class="s">"Error: </span><span class="se">\(</span><span class="n">error</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<h2 id='a-name-example6-a-example-6' class='heading'><a name="example6"></a> Example 6:</h2>

<p>Protocol that has initializer requirements.</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithInitializers</span> <span class="p">{</span>
    <span class="k">var</span> <span class="nv">param</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
    <span class="k">var</span> <span class="nv">other</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>

    <span class="nf">init</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">other</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span>
    <span class="nf">init</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>

<p>Sample test setup:</p>
<pre class="highlight swift"><code><span class="c1">// You can use all required initializers</span>
<span class="k">let</span> <span class="nv">mock1</span> <span class="o">=</span> <span class="kt">ProtocolWithInitializersMock</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">let</span> <span class="nv">mock2</span> <span class="o">=</span> <span class="kt">ProtocolWithInitializersMock</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="nv">other</span><span class="p">:</span> <span class="s">"something"</span><span class="p">)</span>

<span class="c1">// Please hav in mind, that they are only to satisfy protocol requirements</span>
<span class="c1">// there is no logic behind that, and so all properties has to be set manually anyway</span>
<span class="n">mock1</span><span class="o">.</span><span class="n">param</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">mock1</span><span class="o">.</span><span class="n">other</span> <span class="o">=</span> <span class="s">""</span>
<span class="n">mock2</span><span class="o">.</span><span class="n">param</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">mock2</span><span class="o">.</span><span class="n">other</span> <span class="o">=</span> <span class="s">"something"</span>
</code></pre>
<h2 id='a-name-example7-a-example-7' class='heading'><a name="example7"></a> Example 7:</h2>

<p>SwiftyMocky has support for generic protocols:</p>
<h3 id='a-name-example7-1-a-protocol-with-generic-methods' class='heading'><a name="example7.1"></a> Protocol with generic methods</h3>

<p>When working with generic methods, as in following protocol definition, there are some additional requirements and limitations.</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithGenericMethods</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="n">methodWithGeneric</span><span class="o">&lt;</span><span class="kt">T</span><span class="o">&gt;</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="kt">T</span><span class="p">,</span> <span class="nv">rhs</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
    <span class="kd">func</span> <span class="n">methodWithGenericConstraint</span><span class="o">&lt;</span><span class="kt">U</span><span class="o">&gt;</span><span class="p">(</span><span class="nv">param</span><span class="p">:</span> <span class="p">[</span><span class="kt">U</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kt">U</span> <span class="k">where</span> <span class="kt">U</span><span class="p">:</span> <span class="kt">Equatable</span>
<span class="p">}</span>
</code></pre>

<p>Sample test when using generic methods:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithGenericMethodsMock</span><span class="p">()</span>

<span class="c1">// For generics - you have to use .any(ValueType.Type) to avoid ambiguity</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">rhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">false</span><span class="p">))</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">String</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">rhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">String</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">true</span><span class="p">))</span>
<span class="c1">// In that case it is enough to specify type for only one elemen, so the type inference could do the rest</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="nv">rhs</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">true</span><span class="p">))</span>

<span class="c1">// Also, for generics default comparators for equatable and sequence types breaks - so even for</span>
<span class="c1">// simple types like Int or String, you have to register comparator</span>
<span class="c1">// Foe equatable, you can use simplified syntax:</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">)</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">(</span><span class="kt">String</span><span class="o">.</span><span class="k">self</span><span class="p">)</span>

<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="nv">rhs</span><span class="p">:</span> <span class="mi">0</span><span class="p">),</span> <span class="kc">true</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="nv">rhs</span><span class="p">:</span> <span class="mi">1</span><span class="p">),</span> <span class="kc">false</span><span class="p">)</span>
<span class="kt">XCTAssertEqual</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="s">"0"</span><span class="p">,</span> <span class="nv">rhs</span><span class="p">:</span> <span class="s">"1"</span><span class="p">),</span> <span class="kc">true</span><span class="p">)</span>

<span class="c1">// Same applies to verify - specify type to avoid ambiguity</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">rhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">)))</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">.</span><span class="nf">methodWithGeneric</span><span class="p">(</span><span class="nv">lhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">String</span><span class="o">.</span><span class="k">self</span><span class="p">),</span> <span class="nv">rhs</span><span class="p">:</span> <span class="o">.</span><span class="nf">any</span><span class="p">(</span><span class="kt">String</span><span class="o">.</span><span class="k">self</span><span class="p">)))</span>
</code></pre>
<h3 id='a-name-example7-2-a-protocol-with-associated-types' class='heading'><a name="example7.2"></a> Protocol with associated types</h3>

<p>All mocks for protocols with associated types requires additional annotations - one per every associated type. The mock will be generated as generic class.</p>

<p>Protocol definition:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="c1">//sourcery: associatedtype = "T: Sequence"</span>
<span class="kd">protocol</span> <span class="kt">ProtocolWithAssociatedType</span> <span class="p">{</span>
    <span class="kd">associatedtype</span> <span class="kt">T</span><span class="p">:</span> <span class="kt">Sequence</span>

    <span class="k">var</span> <span class="nv">sequence</span><span class="p">:</span> <span class="kt">T</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>

    <span class="kd">func</span> <span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="kt">T</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">}</span>
</code></pre>

<p>Sample test when using mock adopting protocol with associated types:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">mock</span> <span class="o">=</span> <span class="kt">ProtocolWithAssociatedTypeMock</span><span class="o">&lt;</span><span class="p">[</span><span class="kt">Int</span><span class="p">]</span><span class="o">&gt;</span><span class="p">()</span>
<span class="n">mock</span><span class="o">.</span><span class="n">sequence</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>

<span class="c1">// For generics - default comparators for equatable and sequence types breaks - so even for</span>
<span class="c1">// simple types like Int or String, you have to register comparator</span>
<span class="c1">// Foe equatable, you can use simplified syntax:</span>
<span class="kt">Matcher</span><span class="o">.</span><span class="k">default</span><span class="o">.</span><span class="nf">register</span><span class="p">(</span><span class="kt">Int</span><span class="o">.</span><span class="k">self</span><span class="p">)</span>

<span class="c1">// There is autocomplete issue, so in order to get autocomplete for all available methods</span>
<span class="c1">// Use full &lt;MockName&gt;.Given. ... syntax</span>
<span class="kt">Given</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="kt">ProtocolWithAssociatedTypeMock</span><span class="o">.</span><span class="kt">Given</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="o">.</span><span class="n">any</span><span class="p">,</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">false</span><span class="p">))</span>
<span class="c1">// It works slightly better, when using given directly on mock instance</span>
<span class="n">mock</span><span class="o">.</span><span class="nf">given</span><span class="p">(</span><span class="kt">ProtocolWithAssociatedTypeMock</span><span class="o">&lt;</span><span class="p">[</span><span class="kt">Int</span><span class="p">]</span><span class="o">&gt;.</span><span class="kt">Given</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">]),</span> <span class="nv">willReturn</span><span class="p">:</span> <span class="kc">true</span><span class="p">))</span>

<span class="kt">XCTAssertTrue</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">]))</span>
<span class="kt">XCTAssertFalse</span><span class="p">(</span><span class="n">mock</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">]))</span>

<span class="c1">// Similar here</span>
<span class="kt">Verify</span><span class="p">(</span><span class="n">mock</span><span class="p">,</span> <span class="kt">ProtocolWithAssociatedTypeMock</span><span class="o">.</span><span class="kt">Verify</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">])))</span>
<span class="c1">// And also here, using method on instance works slightly better when comes to types inference</span>
<span class="n">mock</span><span class="o">.</span><span class="nf">verify</span><span class="p">(</span><span class="kt">ProtocolWithAssociatedTypeMock</span><span class="o">&lt;</span><span class="p">[</span><span class="kt">Int</span><span class="p">]</span><span class="o">&gt;.</span><span class="kt">Verify</span><span class="o">.</span><span class="nf">methodWithType</span><span class="p">(</span><span class="nv">t</span><span class="p">:</span> <span class="o">.</span><span class="nf">value</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">])))</span>
</code></pre>

          </section>
        </section>
        <section id="footer">
          <p>Copyright © 2017 MakeAWishFoundation. All rights reserved.</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</html>
